﻿
Partial Class pages_BookInfo
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Called when the page first loads.  Checks to see if there's a book ID in the
    ''' URL.  If there is, information about the book is loaded.  Otherwise, there's 
    ''' an error message displayed to the user.
    ''' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ReqBookIDStr As String = Request.QueryString("bid")

        If (Not (ReqBookIDStr Is Nothing)) Then
            ' Request.QueryString.Count will be zero if the query parameter is not present
            ' in the URL.
            Dim BookDataReader As System.Data.OleDb.OleDbDataReader = LibraryBookDataTier.ExecuteSelect("SELECT ISBN, Title, author_Name, copy_No, checkOutStatus FROM Books WHERE book_ID = " & ReqBookIDStr)

            ' Add table
            Dim BookInfoTable As WebControls.Table = New WebControls.Table

            ' We only care about the first result.
            BookDataReader.Read()

            Dim ISDNCell As WebControls.TableCell = New WebControls.TableCell
            ISDNCell.Text = BookDataReader(0).ToString
            Dim TitleCell As WebControls.TableCell = New WebControls.TableCell
            TitleCell.Text = BookDataReader(1).ToString
            Dim AuthorCell As WebControls.TableCell = New WebControls.TableCell
            AuthorCell.Text = BookDataReader(2).ToString

            ' Checkout option
            Dim CheckOutOptionCell As WebControls.TableCell = New WebControls.TableCell
            Dim CheckOutHyperlink As WebControls.HyperLink = New WebControls.HyperLink
            CheckOutHyperlink.Text = "Check out"
            CheckOutHyperlink.Attributes.Add("href", "Common/CheckOutBook.aspx?bid=" & ReqBookIDStr & "&op=c")
            CheckOutOptionCell.Controls.Add(CheckOutHyperlink)

            ' Reserve option
            Dim RsvOptionCell As WebControls.TableCell = New WebControls.TableCell
            Dim RsvHyperlink As WebControls.HyperLink = New WebControls.HyperLink
            RsvHyperlink.Text = "Reserve"
            RsvHyperlink.Attributes.Add("href", "Common/CheckOutBook.aspx?bid=" & ReqBookIDStr & "&op=r")
            RsvOptionCell.Controls.Add(RsvHyperlink)

            Dim ContainingRow As WebControls.TableRow = New WebControls.TableRow
            ContainingRow.Cells.Add(ISDNCell)
            ContainingRow.Cells.Add(TitleCell)
            ContainingRow.Cells.Add(AuthorCell)
            ContainingRow.Cells.Add(CheckOutOptionCell)
            ContainingRow.Cells.Add(RsvOptionCell)

            BookInfoTable.Rows.Add(ContainingRow)
            BookInfoTable.Attributes.Add("class", "GenericTable")

            BookInfoOrError.Controls.Add(BookInfoTable)
        Else
            Dim errorMsg As WebControls.Label = New WebControls.Label
            errorMsg.Attributes.Add("class", "error")
            errorMsg.Text = "Book not found."

            Me.BookInfoOrError.Controls.Add(errorMsg)
        End If

    End Sub
End Class
